package com.zis.auction.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zis.auction.dao.EssayDao;
import com.zis.auction.model.Essay;
import com.zis.auction.service.IEssayInter;
import com.zis.base.impl.BaseInterImpl;
import com.zis.core.Constant;
import com.zis.core.ResponseEntity;
import org.beetl.sql.core.SQLManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Create by wangshen 2018/1/12
 */
@Service
@Transactional("txManager")
public class EssayInterImpl extends BaseInterImpl<Essay> implements IEssayInter {

    @Autowired
    private SQLManager sqlManager;

    @Autowired
    private EssayDao essayDao;

    @Override
    public String insert(Essay essay) throws Exception {
        essay.setCreateDate(new Date());
        essay.setReleaseDate(new Date());
        int num = sqlManager.insert(essay);
        if(num > 0){
            return ResponseEntity.createNormalJsonResponse(Constant.DATA_RESULT_SUCCESS);
        }
        return ResponseEntity.createErrorJsonResponse(Constant.DATA_RESULT_ERROR);
    }

    @Override
    public String update(Essay essay) throws Exception {
        essay.setUpdateDate(new Date());
        int num = sqlManager.updateTemplateById(essay);
        if(num > 0){
            return ResponseEntity.createNormalJsonResponse(Constant.DATA_RESULT_SUCCESS);
        }
        return ResponseEntity.createErrorJsonResponse(Constant.DATA_RESULT_ERROR);
    }

    @Override
    public String delete(String id) throws Exception {
        int num = sqlManager.deleteById(Essay.class, id);
        if(num > 0){
            return ResponseEntity.createNormalJsonResponse(Constant.DATA_RESULT_SUCCESS);
        }
        return ResponseEntity.createErrorJsonResponse(Constant.DATA_RESULT_ERROR);
    }

    @Override
    public Essay selectById(String id) throws Exception {
        return essayDao.findById(id);
    }

    @Override
    public List<Essay> findByCondition(Essay essay) throws Exception {
        return sqlManager.template(essay);
    }

    @Override
    public String findByPage(String msgType, Integer page, Integer pageSize) throws Exception {
        List<Essay> essayList = essayDao.findByPage(msgType, page, pageSize);
        if (essayList!=null&&essayList.size()>0) {
            JSONObject result = new JSONObject();
            result.put("data", essayList);
            result.put("page", page);
            result.put("pageSize", pageSize);
            return ResponseEntity.createNormalJsonResponse(result);
        }
        return ResponseEntity.createNULLJsonResponse("没有数据～");
    }
}
